package chances.webui.taglibs;

import java.util.Objects;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.springframework.util.Assert;

import chances.webui.model.Column;

@SuppressWarnings("serial")
public class ColumnTag extends BodyTagSupport {
	private Column column;

	public void setStyle(String style) {
		column.setStyle(style);
	}

	@Override
	public void setPageContext(PageContext pageContext) {
		super.setPageContext(pageContext);
		column = new Column();
	}

	@Override
	public int doEndTag() throws JspException {
		if (column.getFormat() == null && this.bodyContent != null) {
			column.setFormat(this.getBodyContent().getString());
		}
		TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class);
		
		Assert.notNull(tableTag);
		tableTag.getTable().addColumn(column);
		if(Objects.nonNull(column.getLookup())){
			tableTag.getTable().addLookup(column.getField()+"Lookup", column.getLookup());
		}
		return super.doEndTag();
	}

	public void setStyleClass(String styleClass) {
		column.setStyleClass(styleClass);
	}

	public void setTitle(String title) {
		column.setTitle(title);
	}

	public void setField(String field) {
		column.setField(field);
	}

	public void setFormat(String format) {
		column.setFormat(format);
	}

	public void setSort(String sort) {
		column.setSort(sort);
	}

	public void setAlign(String align) {
		column.setAlign(align);
	}

	public void setValign(String valign) {
		column.setValign(valign);
	}

	public void setEditor(String editor) {
		column.setEditor(editor);
	}

	public void setLookup(Object lookup) {
		column.setLookup(lookup);
	}

	public void setKeyFiled(boolean keyFiled) {
		column.setKeyFiled(keyFiled);
	}
	public void setInput(String input) {
		column.setInput(input);
	}

	
	public void setSearchable(boolean searchable) {
		column.setSearchable(searchable);
	}

	public boolean isRequired() {
		return column.isRequired();
	}

	public void setRequired(boolean required) {
		column.setRequired(required);
	}

	public String getField() {
		return column.getField();
	}

    public boolean isEditReadOnly() {
        return column.isEditReadOnly();
    }

    public void setEditReadOnly(boolean editReadOnly) {
        column.setEditReadOnly(editReadOnly);
    }

    public boolean isAppendReadOnly() {
        return column.isAppendReadOnly();
    }

    public void setAppendReadOnly(boolean appendReadOnly) {
        column.setAppendReadOnly(appendReadOnly);
    }
}
